package User;

import MySQL_im.getInfo;
import Warning.ShowUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class CreateSignUI extends JFrame implements MouseListener{
    ImageIcon img;
    getInfo info = new getInfo();
    JTextField inputname=new JTextField();//设置输入用户名的文本框
    JPasswordField inputpass=new JPasswordField();//设置输入密码的文本框
    JPasswordField inputpass_a = new JPasswordField();
    String sexR;
    ButtonGroup Sexch;
    JRadioButton man,female;
    JTextField inputbirth = new JTextField();
    JLabel name=new JLabel("用户名：");
    JLabel pass=new JLabel("密码：");
    JLabel pass_a=new JLabel("确定密码：");
    JLabel sex = new JLabel("性别：");
    JLabel birth = new JLabel("出生日期：");
    JButton btn=new JButton("确定");
    int width = 500,height = 500;
    public CreateSignUI(String path,String t){
        img = new ImageIcon(path);
        JLabel title = new JLabel(t,SwingConstants.CENTER);
        title.setFont(new Font("华文行楷",Font.BOLD,26));
        JLabel label=new JLabel(img);
        label.setSize(img.getIconWidth(),img.getIconHeight());
        label.setBounds(0,0, img.getIconWidth(), img.getIconHeight());
        title.setBounds(width/8,height/10,width/4*3,height/10);
        Sexch = new ButtonGroup();
        man = new JRadioButton("男");
        female = new JRadioButton("女");
        Sexch.add(man);
        Sexch.add(female);
        setSize(width,height);
        setTitle("注册列表");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        btn.setBounds(width/4,height/5+200,width/2,height/10);
        getContentPane().add(btn);
        //添加用户名、密码、性别、出生日期
        name.setBounds(height/5,height/5,width/5,height/15);
        getContentPane().add(name);
        pass.setBounds(height/5,40+height/5,width/5,height/15);
        getContentPane().add(pass);
        pass_a.setBounds(height/5,80+height/5,width/5,height/15);
        getContentPane().add(pass_a);
        sex.setBounds(height/5,120+height/5,width/5,height/15);
        getContentPane().add(sex);
        birth.setBounds(height/5,160+height/5,width/5,height/15);
        getContentPane().add(birth);
        //添加文本框
        inputname.setBounds(height/5*2,height/5,width/2,height/15);
        inputpass.setBounds(width/5*2,height/5+40,width/2,height/15);
        inputpass_a.setBounds(width/5*2,height/5+80,width/2,height/15);
        man.setBounds(width/5*2,height/5+110,width/10,height/10);
        man.setOpaque(false);
        female.setBounds(width/5*2+80,height/5+110,width/10,height/10);
        female.setOpaque(false);
        inputbirth.setBounds(width/5*2,height/5+160,width/2,height/15);
        getContentPane().add(inputname);
        getContentPane().add(inputpass);
        getContentPane().add(inputpass_a);
        getContentPane().add(man);
        getContentPane().add(female);
        getContentPane().add(inputbirth);
        getContentPane().add(title);
        getContentPane().add(label);
//        添加点击事件
        btn.addMouseListener(this);
        setVisible(true);
    }
    public void mouseClicked(MouseEvent e){
        Object obj = e.getSource();
        if(obj==btn){
            info.Found(inputname.getText(),String.valueOf(inputpass.getPassword()),"注册");
            if(!Arrays.equals(inputpass_a.getPassword(), inputpass.getPassword())){
                ShowUI showUI = new ShowUI("警告","两次输入密码不一致，请重输！");
            }
            else{
                if(info.found){
                    ShowUI showUI = new ShowUI("注册失败","该用户名已存在！");
                }else{
                    if(man.isSelected())
                        sexR = "男";
                    if(female.isSelected())
                        sexR="女";
                    if(inputname.getText().isEmpty() || inputbirth.getText().isEmpty() || sexR.isEmpty()){
                        ShowUI showUI = new ShowUI("警告","请完善个人信息！");
                    }else{
                        info.CreateDatabase(inputname.getText());
                        info.Add(inputname.getText(),String.valueOf(inputpass.getPassword()),sexR,inputbirth.getText());
                        dispose();
                    }
                }
            }
        }
    }
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}


